package org.framework.common.api.service.repository;

import java.io.Serializable;

import javax.persistence.EntityManager;

import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.util.Assert;

/**
 * Spring Data JPA 自定义工厂
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年1月20日 上午11:10:52
 * @version 1.0.2016
 */
public class CommonRepositoryFactory extends JpaRepositoryFactory {

	private final EntityManager entityManager;
	
	public CommonRepositoryFactory(EntityManager em) {
		super(em);
		Assert.notNull(em);  
        this.entityManager = em;  
	}

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected <T, ID extends Serializable> SimpleJpaRepository<?, ?> getTargetRepository(RepositoryInformation information, EntityManager em) {
		JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());
		return new CommonRepositoryDefault(entityInformation, em);
	}

	@Override
	protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
		return CommonRepositoryDefault.class;
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}
}
